/**
 * Copyright © 2015-2020 <a href="http://www.jeeplus.org/">JeePlus</a> All rights reserved.
 */
package com.jeeplus.modules.brand.web;

import java.util.Comparator;
import java.util.List;

import org.apache.commons.collections4.CollectionUtils;
import org.apache.shiro.authz.annotation.Logical;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.jeeplus.common.json.AjaxJson;
import com.jeeplus.core.web.BaseController;
import com.jeeplus.common.utils.StringUtils;
import com.jeeplus.modules.brand.entity.Brand;
import com.jeeplus.modules.brand.service.BrandService;

/**
 * 品牌Controller
 * @author zhych
 * @version 2021-12-08
 */
@RestController
@RequestMapping(value = "/brand/brand")
public class BrandController extends BaseController {

	@Autowired
	private BrandService brandService;

	@ModelAttribute
	public Brand get(@RequestParam(required=false) String id) {
		Brand entity = null;
		if (StringUtils.isNotBlank(id)){
			entity = brandService.get(id);
		}
		if (entity == null){
			entity = new Brand();
		}
		return entity;
	}


	/**
	 * 品牌树表数据
	 */
	@RequiresPermissions("brand:brand:list")
	@GetMapping("list")
	public AjaxJson list(Brand brand) {
		return AjaxJson.success().put("list", brandService.findList(brand));
	}

	/**
	 * 根据Id获取品牌数据
	 */
	@RequiresPermissions(value={"brand:brand:view","brand:brand:add","brand:brand:edit"},logical=Logical.OR)
	@GetMapping("queryById")
	public AjaxJson queryById(Brand brand) {
		return AjaxJson.success().put("brand", brand);
	}

	/**
	 * 保存品牌
	 */
	@RequiresPermissions(value={"brand:brand:add","brand:brand:edit"},logical=Logical.OR)
	@PostMapping("save")
	public AjaxJson save(Brand brand, Model model) throws Exception{
		/**
		 * 后台hibernate-validation插件校验
		 */
		String errMsg = beanValidator(brand);
		if (StringUtils.isNotBlank(errMsg)){
			return AjaxJson.error(errMsg);
		}
		//新增或编辑表单保存
		brandService.save(brand);//保存
		return AjaxJson.success("保存品牌成功");
	}

	/**
	 * 删除品牌
	 */
	@RequiresPermissions("brand:brand:del")
	@DeleteMapping("delete")
	public AjaxJson delete(Brand brand) {
		brandService.delete(brand);
		return AjaxJson.success("删除品牌成功");
	}

	/**
     * 获取JSON树形数据。
     * @param extId 排除的ID
     * @return
	*/
	@RequiresPermissions("user")
	@GetMapping("treeData")
	public AjaxJson treeData(@RequestParam(required = false) String extId) {
		List<Brand> list = brandService.findList(new Brand());
		List<Brand> rootTree =  brandService.formatListToTree (new Brand ("0"),list, extId );
		rootTree.sort(Comparator.comparing(Brand::getSort));
		for (Brand brand : rootTree) {
			if (CollectionUtils.isNotEmpty(brand.getChildren())) {
				for (Brand child : brand.getChildren()) {
					if (CollectionUtils.isNotEmpty(child.getChildren())) {
						child.getChildren().sort(Comparator.comparing(Brand::getYearStyle));
					}
				}
			}
		}
		return AjaxJson.success().put("treeData", rootTree);
	}

}